﻿using God.ShoppingWebApi.DTO;
using God.ShoppingWebApi.Entities;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace God.ShoppingWebApi.WebAdmin
{
    /// <summary>
    /// 角色用户表
    /// </summary>
    public class AdminRoleUserService : ApplicationService, IAdminRoleUserService
    {
        IRepository<UserRoles, Guid> _userroles;
        IRepository<Roles, Guid> _roles;
        IRepository<UserLogin, Guid> _user;
        public AdminRoleUserService(IRepository<UserRoles, Guid> userroles, IRepository<Roles, Guid> roles, IRepository<UserLogin, Guid> user)
        {
            _userroles = userroles;
            _roles = roles;
            _user = user;
        }
        [Route("api/UserRoleAdd")]
        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="roleUsers"></param>
        /// <returns></returns>
        public async Task AddAsync(RoleUsersDTO roleUsers)
        {
            UserRoles ci = new UserRoles(
               GuidGenerator.Create(),

                          roleUsers.UserId,
                         roleUsers.RoleId,
                    
               CurrentUser.Id);

            await _userroles.InsertAsync(ci);
        }
        [Route("api/UserRoleDel")]
        /// <summary>
        /// 删除角色用户
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid gu)
        {

            await _userroles.DeleteAsync(gu);
        }
        [HttpGet]
        [Route("api/UserRoleEdit")]
        /// <summary>
        /// 反填角色用户
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        public async Task<RoleUsersDTO> Edit(Guid gid)
        {
            var data = await _userroles.GetAsync(gid);
            var RoleUsersDTO = ObjectMapper.Map<UserRoles, RoleUsersDTO>(data);
            return RoleUsersDTO;
        }
        [HttpGet]
        [Route("api/UserRoleShow")]
        /// <summary>
        /// 角色用户显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleUsersDTO>> GetListAsync()
        {
            //var str = (from x in await _userroles.GetListAsync()
            //           join a in await _user.GetListAsync()
            //           on x.Id equals a.Id
            //           join b in await _roles.GetListAsync()
            //           on a.Id equals b.Id
            //           select new RoleUsersDTO
            //           {
            //               Id = x.Id,
            //               RoleId = x.RoleId,
            //               UserId = x.UserId,
            //               UserName=a.UserName,
            //               RoleName=b.RoleName

            //           });
            //return str.ToList();
            var str = await _userroles.GetListAsync();
            return ObjectMapper.Map<List<UserRoles>, List<RoleUsersDTO>>(str);
        }
        [Route("api/UserRoleDel")]
        /// <summary>
        /// 修改用户角色
        /// </summary>
        /// <param name="roleUsers"></param>
        /// <returns></returns>
        public async Task<RoleUsersDTO> Update(RoleUsersDTO roleUsers)
        {
            var data = await _userroles.GetAsync(roleUsers.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.RoleId = roleUsers.RoleId;
                data.UserId = roleUsers.UserId;
                var Info = await _userroles.UpdateAsync(data);
                return roleUsers;
            }
            else
            {
                return null;
            }
        }
    }
}
